//
//  SEGPublishHouseTagViewModel.m
//  Commom
//
//  Created by 梁学彰 on 2020/6/4.
//

#import "SEGPublishHouseTagViewModel.h"
NSString * const SEGPublishHouseTagViewCellIndetifier = @"SEGPublishHouseTagViewCellIndetifier";
static CGFloat ContainerLeftRightMargin = 15;
static CGFloat ContainerBottomMargin = 10;
static CGFloat TagHeight = 30;
static CGFloat TagsMargin = 10;
static CGFloat TagInsideLeftRightMargin = 13;

@implementation SEGPublishHouseTagViewModel
-(instancetype)init{
    if (self == [super init]) {
        _title = @"";
        _widgetWidth = [UIScreen mainScreen].bounds.size.width;
        _widgetHeight = kPublishHouseTagViewHeight;
    }
    return self;
}
@end

/**************************SEGPublishHouseTagViewLayout************************/
@implementation SEGPublishHouseTagViewLayout
- (instancetype)initWithData:(SEGPublishHouseTagViewModel *)data type:(SEGEnumPublishHouseTagViewType)type{
    if (self == [super init]) {
        _data = data;
        _type = type;
    }
    [self segPublishHouseTagViewLayout];
    return self;
}

-(void)segPublishHouseTagViewLayout{
    //高度固定
    _totalHeight = _data.widgetHeight;
    
    //高度不固定
    //_totalHeight = 0;
    _titleLabelHeight = 0;
    _titleLabelTextLayout = nil;
    
    if (!_data) return;
    
    [self layout];
}

//数据处理
-(void)layout{
    UIFont *font = [UIFont systemFontOfSize:14];
    
    __block CGFloat W = 0;
    __block CGFloat X = 15;
    __block NSInteger currentLine = 0;
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width - ContainerLeftRightMargin;
    
    NSMutableArray *arrM = [NSMutableArray array];
    [self.data.tags enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull objA, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *obj = [objA valueForKey:@"name"];
        CGFloat objWidth = [obj widthForFont:font] + TagInsideLeftRightMargin*2 + TagsMargin;
        W += objWidth;
        
        if (W >= screenWidth) {
            currentLine += 1;
            W = objWidth;
            X = 15;
        }
        CGRect rect = CGRectMake(X, currentLine * (TagHeight + ContainerBottomMargin), [obj widthForFont:font] + TagInsideLeftRightMargin*2,TagHeight);
        X += objWidth;
        NSMutableDictionary *ret = [NSMutableDictionary dictionaryWithDictionary:objA];
        [ret setValue:NSStringFromCGRect(rect) forKey:@"rect"];
        [arrM addObject:ret];
    }];
    CGFloat tagsTotalHeight = self.data.tags.count > 0 ?  (currentLine+1) * (TagHeight + ContainerBottomMargin) : 0;
    
    _totalHeight = tagsTotalHeight + 50 + 10;
    _tagsTotalHeight = tagsTotalHeight;
    self.tagsData = [NSArray arrayWithArray:arrM];
    [self segtitleLabelWithContent:self.data.title fontSize:15 textColor:kColor_1];
}
//控件titleLabel文本布局计算
-(void)segtitleLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    //modifier.lineHeightMultiple = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(_data.widgetWidth, HUGE);
    container.linePositionModifier = modifier;
    container.truncationType = SEGTextTruncationTypeEnd;// "abcd…"
    
    _titleLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_titleLabelTextLayout)return;
    _titleLabelHeight = [modifier heightForLineCount:_titleLabelTextLayout.rowCount];
}

- (void)refreshTagWithArray:(NSArray <NSDictionary *>*)tagArray {
    for (NSDictionary *dic in tagArray) {
        for (NSMutableDictionary *dict in self.tagsData) {
            if ([dict[@"code"] isEqualToString:dic[@"code"]]) {
                [dict setValue:dic[@"isSelected"] forKey:@"isSelected"];
                break;
            }
        }
    }
}

@end

